//
//  ForgetPwdVC.m
//  KeleAppProject
//
//  Created by 周岑 on 2018/1/12.
//  Copyright © 2018年 Cen Zhou. All rights reserved.
//

#import "ForgetPwdVC.h"
#import "RegisterView.h"
@interface ForgetPwdVC ()

@property (nonatomic, strong) RegisterView *forgetPwdView;
@end

@implementation ForgetPwdVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"忘记密码";
    self.view.backgroundColor = [UIColor whiteColor];
    [self loadSubviewsOnForgetVc];
}

- (void)loadSubviewsOnForgetVc{
    [self.view addSubview:self.forgetPwdView];
    [self.forgetPwdView.registerBtn setTitle:@"确定" forState:UIControlStateNormal];
    UIView *lineView = [[UIView alloc]init];
    lineView.backgroundColor = KLColor_B3;
    [self.view addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.mas_equalTo(1);
    }];
    
    
    // 注册
    self.forgetPwdView.delegateCode = [RACSubject subject];
   
    [self.forgetPwdView.delegateCode subscribeNext:^(NSDictionary *x) {
       
        
        NSString *phoneStr = x[@"phoneStr"];
        if (phoneStr.length<=0) {
            [SVProgressHUD showAlert:@"手机号不能为空"];
        }else if(![CheckMobile isPhoneNo:phoneStr]){
            [SVProgressHUD showAlert:@"请输入正确手机号"];
        }else{
            
            [[KeleData sharedInstance] sendPhoneCodeWithPhone:x[@"phoneStr"] success:^(id responseObject) {
                if ([responseObject[@"success"] integerValue] == 1) {
                    [[NSUserDefaults standardUserDefaults] setObject:responseObject[@"error"] forKey:@"sessionId"];
                }
            } failure:^(NSError *error) {
                NSLog(@"login->sendcode->error->%@",error);
            }];
        }
        
    }];
    
    self.forgetPwdView.delegateSignal = [RACSubject subject];
    [self.forgetPwdView.delegateSignal subscribeNext:^(NSDictionary *x) {
        [SVProgressHUD showLoading:@"更改中..."];
        [[KeleData sharedInstance] forgetPasswordWithPhone:x[@"phoneStr"] sessionId:SessionID code:x[@"codeStr"] password:x[@"pwd"] success:^(id responseObject) {
            
            [SVProgressHUD dismiss];
            if ([responseObject[@"success"] integerValue] == 1) {
                [self dismissViewControllerAnimated:YES completion:nil];
            }else{
                NSLog(@"login->register->error");
            }
            
        } failure:^(NSError *error) {
            [SVProgressHUD dismiss];
            NSLog(@"login->register->error->%@",error);
        }];
    }];
    
}


// lazyload
- (RegisterView *)forgetPwdView{
    if (!_forgetPwdView) {
        _forgetPwdView = [[RegisterView alloc]initWithFrame:CGRectMake(0,0, KLScreenW, KLScreenH-NaviH)];
        _forgetPwdView.backgroundColor = [UIColor whiteColor];
    }
    return _forgetPwdView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
